<?php
require "Db\DbHelper.php";
require "Tools\WebResult.php";

use Tools\WebResult;
use Db\DbHelper;

$user_name = $_REQUEST["user_name"];
$user_pass = $_REQUEST["user_pass"];

$db = DBHelper::createPDO();

// 查找同名用户
$stmt = $db->prepare('select * from user where name = ?');
$stmt->bindValue(1, $user_name);

$stmt->execute();
$row = $stmt->fetch();

// 判断数据库中是不是已经存在用户
if ($row) {
    WebResult::JsonFail('Account already existed！');
}

// 用户不存在
$stmt = $db->prepare('insert into user(name, pwd) values(?, ?)');
$stmt->bindValue(1, $user_name);
$stmt->bindValue(2, $user_pass);

$stmt->execute();

WebResult::JsonSuccess("Account created！");

